from Faux_vers_vrai import get_solution_FV
from Vrai_vers_faux import get_solution
import numpy as N

def Collage():

	epsilon = 0.01

	t_start = 0.0005
	pas     = 0.0005

	# Faux_vers_vrai

	t_final = 210.5

	phi0_M  = -7.94071084819e-91
	phi0_m  = -7.9407108454e-91
	phi0    = (phi0_M+phi0_m)/2.

	tvec_FV,yvec_FV = get_solution_FV(t_start,t_final,phi0,(t_final-t_start)/pas,epsilon)

	# Vrai_vers_faux

	t_final = 30.

	phi0_M  = 3.54103147816e-05
	phi0_m  = 3.54103146894e-05
	phi0    = (phi0_M+phi0_m)/2.

	tvec,yvec = get_solution(t_start,t_final,phi0,(t_final-t_start)/pas,epsilon)

	p = N.argmax(yvec[:,0])

	D_phi = N.copy(yvec[:p+1,0])
	phi   = N.copy(yvec[:p+1,1])
	D_b   = N.copy(yvec[:p+1,2])
	b     = N.copy(yvec[:p+1,3])

	q = N.argmin(yvec_FV[:,0])

	D_phi_FV = N.copy(yvec_FV[:q,0])
	phi_FV   = N.copy(yvec_FV[:q,1])
	D_b_FV   = N.copy(yvec_FV[:q,2])
	b_FV     = N.copy(yvec_FV[:q,3])

	# Reverse
	D_phi_FV = D_phi_FV[ : :-1]
	phi_FV   = phi_FV[ : :-1]
	D_b_FV   = D_b_FV[ : :-1]
	b_FV     = b_FV[ : :-1]

	D_phi_FV = -D_phi_FV
	phi_FV   = phi_FV + 2. # Position of the FV
	D_b_FV   = -D_b_FV

	# The concatenation would be much better with a small phase considered
	D_phi = N.concatenate([D_phi,D_phi_FV])
	phi   = N.concatenate([phi,phi_FV])
	D_b   = N.concatenate([D_b,D_b_FV])
	b     = N.concatenate([b,b_FV])

	tvec  = N.linspace(0.,D_phi.size-1,D_phi.size)*pas+t_start

	integ = 0.
	f=[integ]
	for i in N.linspace(2,len(tvec),len(tvec)-1):
		vec=tvec[i-2:i].copy()
		bvec=b[i-2:i].copy()
		integ = integ + N.trapz(1./bvec-1./vec,vec) # C'est la methode la plus simple d'integration : utiliser des trapezes
		f.append(integ)

	return (tvec,D_phi,phi,D_b,b,f)
